/*This function receives a event for the calling function and the class where the controls to be validated reside*/
function ValidateAndSubmit(evt, cssClass) {
    var isValid = true;
    // Get Validator & Settings
    var validator = $("#Form").validate();
    var settings = validator.settings;

    // Find the parent control that contains the elements to be validated
    var $group = $(evt.currentTarget).parents(cssClass);

    // Grab all the input elements (minus items listed below)
    $group
.find(":input")
.not(":submit, :reset, :image, [disabled]")
.not(settings.ignore)
.each(function (i, item) {
    // Don't validate items without rules
    if (!validator.objectLength($(item).rules())) {
        return true;
    }

    if (!$(item).valid()) {
        isValid = false;
    }
});

    // If any control is the group fails, prevent default actions (aka: Submit)
    if (!isValid) {
        evt.preventDefault();
        return false;
    }
    return true;
}

function jqCheckAll(id, idToSearch) {
    var itemChecked = $('#' + id).is(':checked');
    $("#" + idToSearch).find(':checkbox').attr('checked', itemChecked).change();
}

function jqToggleCheckboxes(id, idToSearch) {
    var itemChecked = $('#' + id).is(':checked');
    if (itemChecked) {
        $("#" + idToSearch).find(':checkbox').each(function () {
            if (this.id != id) {
                this.checked = false;
                this.change();
            }
        });
    }
}

/***
*	Receives the id of a check box and the id of a item to disable/enable depending if the checkbox
*  is checked or not
**/
function toggleCheckBoxQttField(id, itemToDisable) {
    var itemChecked = $('#' + id).is(':checked');
    if (itemChecked)
        $("#" + itemToDisable).attr("disabled", "disabled");
    else
        $("#" + itemToDisable).removeAttr("disabled");
}

/***
*	Presents a message to the user, in the specified container
**/
function popUpShow(messageContainer, message) {
    $(window).scroll(function (e) {
        $el = messageContainer;

        if ($(this).scrollTop() > 200 && $el.css('position') != 'fixed') {
            messageContainer.css({ 'position': 'fixed', 'top': '0px' });
        }
    });

    var showIt = function () {
        messageContainer.slideUp().empty();
    };

    messageContainer.html(message).slideDown();
    setTimeout(showIt, 3000);
}

/***
*	Notifies the user with a warning
**/
function popUpShowWarning(containerId, message) {
    var messageContainer = $('#' + containerId + ' #_2DoTasks_Notify').first();
    messageContainer.addClass('_2DoTasks_NotifyWarning');
    popUpShow(messageContainer, message);
}

/***
*	Notifies the user with a information
**/
function popUpShowInformation(containerId, message) {
    var messageContainer = $('#' + containerId + ' #_2DoTasks_Notify').first();
    messageContainer.addClass('_2DoTasks_NotifyInfo');
    popUpShow(messageContainer, message);
}

/***
*	Notifies the user with a information
**/
function popUpShowSuccess(containerId, message) {
    var messageContainer = $('#' + containerId + ' #_2DoTasks_Notify').first();
    messageContainer.addClass('_2DoTasks_NotifySuccess');
    popUpShow(messageContainer, message);
}

/***
*	Notifies the user with a Error
**/
function popUpShowError(containerId, message) {
    var messageContainer = $('#' + containerId + ' #_2DoTasks_Notify').first();
    messageContainer.addClass('_2DoTasks_NotifyError');
    popUpShow(messageContainer, message);
}